﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using DevExpress.Data;


public partial class Cashier_Issue_Approve : System.Web.UI.Page
{
   
    string order;
    string proname;
    decimal qty;

    string Stockcode;
    protected void Page_Load(object sender, EventArgs e)
    {
        
        Session["current"] = "GTN Approve";//current page
        if (Session["user"] != null)//check user
        {
            string user = Session["user"].ToString();
            if (user == "viewer")
            {
                Response.Redirect("index.aspx");
            }

        }
        else
        {
            Response.Redirect("index.aspx");
        }

    
        //ASPxGridView2.TotalSummary.Clear();

        
        string b = Request.QueryString.ToString();
        if (b != "")
        {
            ASPxLabel5.Visible = true;
            //ASPxLabel4.Visible = false;
            ASPxLabel5.Text = "Add Stock to Food city Stock Sucessfully!!";
            decimal grnlast = Bestlife.ERP.BusinessRules.B_Issue_App.lastnotappro_issu();
            DropDownList1.SelectedIndex = -1;
            DropDownList1.Text = grnlast.ToString();
            ASPxGridView2.DataBind();
        }
        else
        {


            if (!IsPostBack)
            {
               
                decimal grnlast = Bestlife.ERP.BusinessRules.B_Issue_App.lastnotappro_issu();
                DropDownList1.SelectedIndex = -1;
                DropDownList1.Text = grnlast.ToString();
                ASPxGridView2.DataBind();
                //Session["update"] = null;
                //ASPxSummaryItem totalSummary = new ASPxSummaryItem();
                //totalSummary.FieldName = "Total";
                //totalSummary.ShowInColumn = "Total";
                //totalSummary.SummaryType = SummaryItemType.Sum;
                //ASPxGridView2.TotalSummary.Add(totalSummary);




            }
        }

                //ASPxTextBox1.Text = Bestlife.ERP.BusinessRules.B_GRN.grandtotal(DropDownList1.Text).ToString();
            
        }

        
    
    protected void ASPxGridView2_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        //Bestlife.ERP.Entities.E_PurchaseOrder order = new Bestlife.ERP.Entities.E_PurchaseOrder();
        //order.Approval = Convert.ToString(e.NewValues[""]);
        //order.Contrat = Convert.ToString(e.NewValues[""]);
        //order.Description = Convert.ToString(e.NewValues[""]);
        //order.Due = Convert.ToDateTime(e.NewValues[""]);
        //order.Issu = Convert.ToDateTime(e.NewValues[""]);
        //order.Or_ID1 = Convert.ToString(e.NewValues[""]);
        //order.Proname = Convert.ToString(e.NewValues[""]);
        //order.Qty = Convert.ToDecimal(e.NewValues[""]);
        //order.Supplier = Convert.ToString(e.NewValues[""]);
        //order.Total = Convert.ToDecimal(e.NewValues[""]);
        //order.Unitvalue = Convert.ToDecimal(e.NewValues[""]);

        //Bestlife.ERP.BusinessRules.

    }
    protected void ASPxButton1_Click(object sender, EventArgs e)
    {

        List<Bestlife.ERP.Entities.E_Issue_Ap> listgrn = new List<Bestlife.ERP.Entities.E_Issue_Ap>();

        int i = ASPxGridView2.VisibleRowCount;
        Boolean result = false;
        List<Object> itemid1 = ASPxGridView2.GetSelectedFieldValues("Pro_ID");
        List<Object> itemid = ASPxGridView2.GetSelectedFieldValues("Issue_ID");
        List<Object> itemid2 = ASPxGridView2.GetSelectedFieldValues("Qty");

        List<Object> itemid4 = ASPxGridView2.GetSelectedFieldValues("Sto_ID");
        int j = itemid.Count;
        string user = Session["username"].ToString();
        if (DropDownList2.SelectedValue != "")
        {
            if (DropDownList2.SelectedValue == "YES")
            {
                string approval = DropDownList2.SelectedValue;
                if (itemid.Count == 0 || itemid1.Count == 0 || itemid2.Count == 0 || itemid4.Count == 0)
                {
                    ASPxLabel2.Visible = true;
                    ASPxLabel2.Text = "Please select order items ";
                }
                else
                {
                    foreach (object selectitemid in itemid)
                    {
                        order = selectitemid.ToString();

                    }
                    foreach (object selectitemid1 in itemid1)
                    {
                        proname = selectitemid1.ToString();

                    }
                    foreach (object selectitemid2 in itemid2)
                    {
                        qty = decimal.Parse(selectitemid2.ToString());

                    }

                    foreach (object selectitemid4 in itemid4)
                    {
                        Stockcode = selectitemid4.ToString();

                    }
                    for (int k = 0; k < itemid.Count; k++)
                    {
                        Bestlife.ERP.Entities.E_Issue_Ap grn = new Bestlife.ERP.Entities.E_Issue_Ap();
                        grn.Stor_ID= itemid4[k].ToString();
                       
                        grn.Issu_ID1 = itemid[k].ToString();
                        grn.Pro_id= itemid1[k].ToString();
                        grn.Qty = decimal.Parse(itemid2[k].ToString());
                        listgrn.Add(grn);

                    }
                    if (Session["update"] != null)
                    {
                        string user1 = Session["username"].ToString();
                        ASPxSummaryItem totalSummary = new ASPxSummaryItem();
                        totalSummary.FieldName = "Total";
                        totalSummary.SummaryType = SummaryItemType.Sum;
                        ASPxGridView2.TotalSummary.Add(totalSummary);
                        string totvalue = ASPxGridView2.GetTotalSummaryValue(totalSummary).ToString();
                        Bestlife.ERP.Entities.E_Issue_Ap grn = new Bestlife.ERP.Entities.E_Issue_Ap();
                        grn.Tot = Convert.ToDecimal(totvalue);
                        grn.Issu_ID1= DropDownList1.Text;
                      //  Bestlife.ERP.BusinessRules.B_GRN.upd_GRNbytot(grn, user1);
                    }
                }
                string approve = "YES";
                if (i == j)
                {


                    result = Bestlife.ERP.BusinessRules.B_Issue_App.allapprove_issu(listgrn, user, approve, itemid.Count);
                    if (result == false)
                    {
                        ASPxLabel2.Visible = true;
                        ASPxLabel2.Text = "Error in the Process";
                    }

                }
                else
                {
                    string approve1;
                    int countrow = ASPxGridView2.VisibleRowCount;
                    int listcount = listgrn.Count;
                    if (countrow == listcount)
                    {
                        approve1 = "YES";
                    }
                    else
                    {
                        approve1 = "HALF";
                    }

                    foreach (Bestlife.ERP.Entities.E_Issue_Ap listgn in listgrn)
                    {

                        result = Bestlife.ERP.BusinessRules.B_Issue_App.approve_issu(listgn, user, approve1, itemid.Count);
                    }

                }
                if (result)
                {

                    Response.Redirect("Issue_Approve.aspx?a=1");
                }
            }

            //else if (DropDownList2.SelectedValue == "NO")
            //{
            //   string approval = DropDownList2.SelectedValue;
            //    if (itemid.Count == 0 || itemid1.Count == 0 || itemid2.Count == 0 ||itemid4.Count == 0)
            //    {
            //        ASPxLabel4.Visible = true;
            //        ASPxLabel4.Text = "Please select order items ";
            //    }
            //    else
            //    {
            //        foreach (object selectitemid in itemid)
            //        {
            //            order = selectitemid.ToString();

            //        }
            //            foreach (object selectitemid1 in itemid1)
            //            {
            //                proname = selectitemid1.ToString();

            //            }
            //                foreach (object selectitemid2 in itemid2)
            //                {
            //                    qty = decimal.Parse(selectitemid2.ToString());

            //                }

            //                foreach (object selectitemid4 in itemid4)
            //                {
            //                    Stockcode = selectitemid4.ToString();

            //                }
            //                for (int k = 0; k < itemid.Count; k++)
            //                {
            //                    Bestlife.ERP.Entities.E_GRN grn = new Bestlife.ERP.Entities.E_GRN();
            //                    grn.Stockid = itemid4[k].ToString();

            //                    grn.Order = itemid[k].ToString();
            //                    grn.Proname = itemid1[k].ToString();
            //                    grn.Qty =decimal.Parse(itemid2[k].ToString());
            //                    listgrn.Add(grn);

            //                }
            //                if (Session["update"] != null)
            //                {
            //                    string user1 = Session["username"].ToString();
            //                    ASPxSummaryItem totalSummary = new ASPxSummaryItem();
            //                    totalSummary.FieldName = "Total";
            //                    totalSummary.SummaryType = SummaryItemType.Sum;
            //                    ASPxGridView2.TotalSummary.Add(totalSummary);
            //                    string totvalue = ASPxGridView2.GetTotalSummaryValue(totalSummary).ToString();
            //                    Bestlife.ERP.Entities.E_PurchaseOrder grn = new Bestlife.ERP.Entities.E_PurchaseOrder();
            //                    grn.Total = Convert.ToDecimal(totvalue);
            //                    grn.Or_ID1 = DropDownList1.Text;
            //                    Bestlife.ERP.BusinessRules.B_GRN.upd_GRNbytot(grn, user1);
            //                }
            //            }
            //    string approve = "NO";
            //    if (i == j)
            //    {


            //        result = Bestlife.ERP.BusinessRules.B_PurchaseOrder.allapprove(listgrn, user, approve, itemid.Count);
            //        if (result == false)
            //        {
            //            ASPxLabel4.Visible = true;
            //            ASPxLabel4.Text = "Error in the Process";
            //        }

            //    }
            //    else
            //    {
            //        string approve1;
            //        int countrow = ASPxGridView2.VisibleRowCount;
            //        int listcount = listgrn.Count;
            //        if (countrow == listcount)
            //        {
            //            approve1 = "NO";
            //        }
            //        else
            //        {
            //            approve1 = "HALF NO";
            //        }

            //        foreach (Bestlife.ERP.Entities.E_GRN listgn in listgrn)
            //        {

            //            result = Bestlife.ERP.BusinessRules.B_PurchaseOrder.approval(listgn, user, approve1, itemid.Count);
            //        }

            //    }
            //    if (result)
            //    {

            //        Response.Redirect("SM_ApprovGRN.aspx?a=1");
            //    }
            //        }
        }
        else
        {
            ASPxLabel2.Visible = true;
            ASPxLabel2.Text = "Please select one option for approve ";
        }




    }

    public void supplier_details(string grnno)
    {
        Bestlife.ERP.Entities.E_GRN grn = new Bestlife.ERP.Entities.E_GRN();
        grn = Bestlife.ERP.BusinessRules.B_GRN.getGRNdetails(grnno);

        //DropDownList3.Text = grn.Supplier;
       
    }
    protected void ASPxGridView2_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
    {
        //if (e.Column.FieldName == "Total")
        //{
        //    decimal unitprice = Convert.ToDecimal(e.GetListSourceFieldValue("Pur_Price"));
        //    decimal Quntity = Convert.ToDecimal(e.GetListSourceFieldValue("Qty"));
        //    decimal tot = unitprice * Quntity;
        //    e.Value = tot;

        //}

    }
    protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
    {

        // ASPxTextBox1.Text = Bestlife.ERP.BusinessRules.B_GRN.grandtotal(DropDownList1.SelectedValue).ToString();
       
        ASPxSummaryItem totalSummary = new ASPxSummaryItem();
        totalSummary.FieldName = "Total";
        totalSummary.ShowInColumn = "Total";
        totalSummary.SummaryType = SummaryItemType.Sum;
        totalSummary.DisplayFormat = "Total Rs.{0}";
        ASPxGridView2.TotalSummary.Add(totalSummary);

    }
    protected void ASPxGridView2_RowUpdating1(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {

        Session["update"] = "1";
        ASPxSummaryItem totalSummary = new ASPxSummaryItem();
        totalSummary.FieldName = "Total";
        totalSummary.ShowInColumn = "Total";
        totalSummary.SummaryType = SummaryItemType.Sum;
        totalSummary.DisplayFormat = "Total Rs.{0}";
        ASPxGridView2.TotalSummary.Add(totalSummary);
    }
    protected void ASPxGridView2_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
        Session["update"] = "1";
        ASPxSummaryItem totalSummary = new ASPxSummaryItem();
        totalSummary.FieldName = "Total";
        totalSummary.ShowInColumn = "Total";
        totalSummary.SummaryType = SummaryItemType.Sum;
        totalSummary.DisplayFormat = "Total Rs.{0}";
        ASPxGridView2.TotalSummary.Add(totalSummary);
    }
}